package com.dj.mingkong.admin.biz.core.kaptcha;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

import static com.google.code.kaptcha.Constants.*;

/**
 * 谷歌图片验证码配置
 *
 * @author zenglonghui
 * @date: 2021/12/9 15:13
 */
@Configuration
public class KaptchaConfig {
    @Bean
    public DefaultKaptcha getDefaultKaptcha() {
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        //图片是否有边框
        properties.setProperty(KAPTCHA_BORDER, "no");
        //图片的边框颜色
        properties.setProperty(KAPTCHA_BORDER_COLOR, "34,114,200");
        //图片的大小
        properties.setProperty(KAPTCHA_IMAGE_WIDTH, "120");
        properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "55");
        //文字内容
        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
        //图片背景颜色
        properties.setProperty(KAPTCHA_BACKGROUND_CLR_FROM, "white");
        properties.setProperty(KAPTCHA_BACKGROUND_CLR_TO, "white");

        properties.setProperty(KAPTCHA_WORDRENDERER_IMPL, KaptchaWordRenderer.class.getName());
        properties.setProperty(KAPTCHA_NOISE_IMPL, KaptchaNoise.class.getName());
        // 字体
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "cmr10");
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}
